package com.pap.car.info.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.base.util.string.StringUtilss;
import com.pap.car.agent.ICarAgent;
import com.pap.car.agent.dto.CarInfoTreeDTO;
import com.pap.car.info.auto.entity.Car;
import com.pap.obj.vo.response.ResponseVO;

@RestController
@RequestMapping("/v1/car/info")
public class CarController extends BaseController<Car, ICarAgent> {

	@Resource(name = "carAgent")
	@Override
	public void setService(ICarAgent service) {
		this.service = service;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/add")
	public ResponseVO<Car> add(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "addJSON", required = true) String addJSON) throws Exception {
		
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		Car car = objectMapper.readValue(addJSON, Car.class);
		car.setId(UUID.randomUUID().toString());
		
		
		if(car != null && 
				StringUtilss.isNotEmpty(car.getName())) {
			// 如果前端没有传递过来code值，则默认使用拼音代替
			if(StringUtilss.isEmpty(car.getCode())) {
				car.setCode(PinyinHelper.convertToPinyinString(car.getName(), "", PinyinFormat.WITHOUT_TONE));
			}
			// 处理父级数据
			Car parentcar = service.selectByPrimaryKey(car.getParentId());
			if(parentcar != null) {
				// pathId
				if(StringUtilss.isNotEmpty(parentcar.getPathIds())) {
					car.setPathIds(parentcar.getPathIds() + "," + parentcar.getId());
				} else {
					car.setPathIds(parentcar.getId());
				}
				// pathCode
				if(StringUtilss.isNotEmpty(parentcar.getPathCodes())) {
					car.setPathCodes(parentcar.getPathCodes() + "," + parentcar.getCode());
				} else {
					car.setPathCodes(parentcar.getCode());
				}
				// pathName
				if(StringUtilss.isNotEmpty(parentcar.getPathNames())) {
					car.setPathNames(parentcar.getPathNames() + "," + parentcar.getName());
				} else {
					car.setPathNames(parentcar.getName());
				}
			} 
			car.setCreateIp("0.0.0.0");
			car.setCreateTime(DateUtils.getCurrDateTimeStr());
			car.setCreateUser(loginUserId);
			car.setClientLicenseId(clientLicenseId);
			
			int operationInt = service.insertSelective(car);
			
			return ResponseVO.successdata(operationInt);
			
		} else {
			return ResponseVO.validfail("入参异常，请判断是否含有组织机构名称");
		}

	}
	
	@Override
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/update")
	public ResponseVO<Car> update(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "updateJSON", required = true) String updateJSON) 
					throws IOException, JsonParseException, JsonMappingException {
		
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		Car car = objectMapper.readValue(updateJSON, Car.class);
			
		ResponseVO responseVO = service.updateAndCheckCar(car, loginUserId);
		return responseVO;
	}
	
	@RequestMapping(value = "/selectcarinfotreejson/{bigTypeFlag}")
	public List<CarInfoTreeDTO> selectCarInfoTreeJSON(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String bigTypeFlag) {
		return service.selectCarInfoTreeJSON(clientLicenseId, bigTypeFlag);
	}
}
